//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "PEICurrency.h"
using namespace Cephei::QL::Currencies;
#include <gen/QL/Math/Rounding.h>
#include <gen/QL/Currency.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Currencies::CPEICurrency::CPEICurrency () : CCurrency(CPEICurrency::typeid)
{
    try
    {
#ifdef HANDLE
        _phPEICurrency = NULL;
#endif
        _ppPEICurrency = new boost::shared_ptr<QuantLib::PEICurrency> (new QuantLib::PEICurrency ( ));
        SetCurrency (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppPEICurrency));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Currencies::CPEICurrency::CPEICurrency (boost::shared_ptr<QuantLib::PEICurrency>& childNative, Object^ owner) : CCurrency(CPEICurrency::typeid)
{
#ifdef HANDLE
	_phPEICurrency = NULL;
#endif
	_ppPEICurrency = &childNative;
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppPEICurrency));
}
Cephei::QL::Currencies::CPEICurrency::CPEICurrency (QuantLib::PEICurrency& childNative, Object^ owner) : CCurrency(CPEICurrency::typeid)
{
#ifdef HANDLE
	_phPEICurrency = NULL;
#endif
	_ppPEICurrency = new boost::shared_ptr<QuantLib::PEICurrency> (&childNative);
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppPEICurrency));
    _PEICurrencyOwner = owner;
    _CurrencyOwner = owner;
}

Cephei::QL::Currencies::CPEICurrency::CPEICurrency (CPEICurrency^ copy) : CCurrency(CPEICurrency::typeid)
{
#ifdef HANDLE
	_phPEICurrency = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPEICurrency = new boost::shared_ptr<QuantLib::PEICurrency> (copy->GetShared());
        _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppPEICurrency));
    }
}
Cephei::QL::Currencies::CPEICurrency::CPEICurrency (System::Type^ t) : CCurrency(CPEICurrency::typeid)
{
#ifdef HANDLE
	_phPEICurrency = NULL;
#endif
	if (!t->IsSubclassOf(CPEICurrency::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Currencies::CPEICurrency::CPEICurrency (QuantLib::Handle<QuantLib::PEICurrency>& childNative, Object^ owner)  : CCurrency(CPEICurrency::typeid)
{
	_phPEICurrency = &childNative;
	_ppPEICurrency = &static_cast<boost::shared_ptr<QuantLib::PEICurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppPEICurrency));
    _PEICurrencyOwner = owner;
}
Cephei::QL::Currencies::CPEICurrency::CPEICurrency (QuantLib::Handle<QuantLib::PEICurrency> childNative)  : CCurrency(CPEICurrency::typeid)
{
	_phPEICurrency = &childNative;
	_ppPEICurrency = &static_cast<boost::shared_ptr<QuantLib::PEICurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppPEICurrency));
}
#endif
#ifdef STRUCT
Cephei::QL::Currencies::CPEICurrency::CPEICurrency (QuantLib::PEICurrency childNative)  : CCurrency(CPEICurrency::typeid)
{
#ifdef HANDLE
	_phPEICurrency = NULL;
#endif
	_ppPEICurrency = new boost::shared_ptr<QuantLib::PEICurrency> (new QuantLib::PEICurrency (childNative));
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppPEICurrency));
}
#endif

Cephei::QL::Currencies::CPEICurrency::~CPEICurrency ()
{
    if (_ppPEICurrency != NULL)
    {
	    delete _ppPEICurrency;
        _ppPEICurrency = NULL;
    }
}
Cephei::QL::Currencies::CPEICurrency::!CPEICurrency ()
{
    if (_ppPEICurrency != NULL)
    {
	    delete _ppPEICurrency;
    }
}
QuantLib::PEICurrency& Cephei::QL::Currencies::CPEICurrency::GetReference ()
{
    if (_ppPEICurrency == NULL) throw gcnew NativeNullException ();
	return **_ppPEICurrency;
}
boost::shared_ptr<QuantLib::PEICurrency>& Cephei::QL::Currencies::CPEICurrency::GetShared ()
{
    if (_ppPEICurrency == NULL) throw gcnew NativeNullException ();
	return *_ppPEICurrency;
}
QuantLib::PEICurrency* Cephei::QL::Currencies::CPEICurrency::GetPointer ()
{
    if (_ppPEICurrency == NULL) throw gcnew NativeNullException ();
	return &**_ppPEICurrency;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PEICurrency>& Cephei::QL::Currencies::CPEICurrency::GetHandle ()
{
	if (_phPEICurrency == NULL)
	{
		_phPEICurrency = new Handle<QuantLib::PEICurrency> (*_ppPEICurrency);
	}
	return *_phPEICurrency;
}
#endif
bool Cephei::QL::Currencies::CPEICurrency::HasNative () 
{
	return (_ppPEICurrency != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Currencies::IPEICurrency^ Cephei::QL::Currencies::CPEICurrency_Factory::Create ()
{
    return gcnew CPEICurrency ();
}
